﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.ScheduleJob.SPI
{
    public class ScheduleJobExecContext
    {
        private Dictionary<string, object> contextDic = null;
        public ScheduleJobExecContext()
        {
            contextDic = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
        }

        public string ScheduleJobID { get; set; }
        public ScheduleJobInstance Instance { get; set; }
        /// <summary>
        /// 元数据
        /// </summary>
        public Metadata.Entity.ScheduleJob.ScheduleJob ScheduleJobMetaData { get; set; }
        /// <summary>
        /// 订阅任务
        /// </summary>
        public ScheduleJobSubscription ScheduleJobSubscription { get; set; }
        public JobKey JobKey { get; set; }
        public TriggerKey TriggerKey { get; set; }

        public string GetDescription()
        {
            return $"计划任务{(ScheduleJobMetaData == null ? ScheduleJobID : ScheduleJobMetaData.Code)}的任务实例{(Instance == null ? "" : Instance.ID)}";
        }
        public void AddData(string key, object value)
        {
            if (contextDic == null)
                contextDic = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            contextDic[key] = value;
        }
        public object GetData(string key)
        {
            if (contextDic != null && contextDic.ContainsKey(key))
                return contextDic[key];
            return null;
        }
        public string GetStringData(string key)
        {
            if (contextDic != null && contextDic.ContainsKey(key))
                return Convert.ToString(contextDic[key]);
            return "";
        }
    }
}
